const LogModel = require('../../model/Log');
const moment = require('moment')
const ShowProduct = require('../../model/ShowProduct')

module.exports = {

    //获取日志列表
    changeShowProduct: async(req, res, next) => {
        try {
            console.log(req.body)
            let showProduct = await ShowProduct.findOne({ page: req.body.page }).exec()
            if (showProduct == null) {
                let newShowProduct = new ShowProduct({
                    page: req.body.page,
                    updatedAt: new Date(),
                    image: req.body.image, // link of cdn images
                    isDelete: 0,
                    product: req.body.product, // 小程序内跳转链接或者网页链接,
                })
                await newShowProduct.save()
                return res.ok('创建成功', newShowProduct)
            } else {
                showProduct.updatedAt = new Date()
                showProduct.page = req.body.page
                showProduct.image = req.body.image
                showProduct.product = req.body.product
                await showProduct.save()
                return res.ok('修改成功', showProduct)
            }
        } catch (e) {
            next(e)
        }
    },

    getShowProduct: async(req, res, next) => {
        try {
            let showProduct = await ShowProduct.findOne({ page: req.body.page }).populate({ path: 'product', populate: { path: 'attribute productCategory' } }).exec()
            return res.ok('查询成功', showProduct)
        } catch (e) {
            next(e)
        }
    },

};